package br.com.rubrol.controller;

import java.io.Serializable;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import br.com.rubrol.bean.Function;
import br.com.rubrol.bean.FunctionUserObject;
import br.com.rubrol.util.MessageBundleLoader;

public abstract class AbstractBaseBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private DefaultTreeModel cadastroModel;
	private DefaultTreeModel consultaModel;

	public AbstractBaseBean() {
		init();
	}

	public DefaultTreeModel getCadastroModel() {
		return cadastroModel;
	}

	public void setCadastroModel(DefaultTreeModel cadastroModel) {
		this.cadastroModel = cadastroModel;
	}

	public DefaultTreeModel getConsultaModel() {
		return consultaModel;
	}

	public void setConsultaModel(DefaultTreeModel consultaModel) {
		this.consultaModel = consultaModel;
	}

	private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String title, Function func) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode();
		FunctionUserObject userObject = new FunctionUserObject(node);
		node.setUserObject(userObject);
		userObject.setFunction(func);

		if (title != null) {
			userObject.setText(title);
			userObject.setLeaf(false);
			userObject.setExpanded(true);
			node.setAllowsChildren(true);
		} else {
			userObject.setText(func.getNome());
			userObject.setLeaf(true);
			node.setAllowsChildren(false);
		}
		if (parent != null) {
			parent.add(node);
		}

		return node;
	}

	protected void init() {
		// Cadastro TreeView
		DefaultMutableTreeNode rootNode = addNode(null, "Items", new Function());
		cadastroModel = new DefaultTreeModel(rootNode);
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.maquinas"), "cadastromaquinas"));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.produtos"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.clientes"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.transportadoras"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.fornecedores"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.usuarios"), ""));

		// Consulta TreeView
		rootNode = addNode(null, "Items", new Function());
		consultaModel = new DefaultTreeModel(rootNode);
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.maquinas"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.produtos"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.clientes"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.transportadoras"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.fornecedores"), ""));
		addNode(rootNode, null, new Function(MessageBundleLoader.getMessage("mainpage.westpanel.usuarios"), ""));

		initComponents();
	}
	
	public abstract void initComponents();

}
